/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ohosadvance.tsnackbar;

import com.ohosadvance.topsnackbar.utils.LogUtil;
import com.ohosadvance.topsnackbar.TSnackbar;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

/**
 * MainAbility
 *
 * @since 2021/7/2
 */
public class MainAbility extends Ability {
    private DependentLayout mDependentLayout;
    private BGButton button7;
    private BGButton button6;
    private BGButton button5;
    private BGButton button4;
    private BGButton button3;
    private BGButton button2;
    private BGButton button1;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        getWindow().setStatusBarColor(getColor(ResourceTable.Color_status_title));
        initView();
    }

    private void initView() {
        button1 = (BGButton) findComponentById(ResourceTable.Id_button_example_1);
        button2 = (BGButton) findComponentById(ResourceTable.Id_button_example_2);
        button3 = (BGButton) findComponentById(ResourceTable.Id_button_example_3);
        button4 = (BGButton) findComponentById(ResourceTable.Id_button_example_4);
        button5 = (BGButton) findComponentById(ResourceTable.Id_button_example_5);
        button6 = (BGButton) findComponentById(ResourceTable.Id_button_example_6);
        button7 = (BGButton) findComponentById(ResourceTable.Id_button_example_toolbar);
        mDependentLayout = (DependentLayout) findComponentById(ResourceTable.Id_relative_layout_main);
        setButton1Click();
        setButton2Click();
        setButton3Click();
        setButton4Click();
        setButton5Click();
        setButton6Click();
        setButton7Click();
    }

    private void setButton1Click() {
        button1.setClickedListener(component -> {
            TSnackbar.make(mDependentLayout, "Hello from VSnackBar 1", TSnackbar.LENGTH_LONG)
                    .show();
        });
    }

    private void setButton2Click() {
        button2.setClickedListener(component -> {
            TSnackbar snackbar = TSnackbar
                    .make(mDependentLayout, "Had a snack at Snackbar", TSnackbar.LENGTH_LONG)
                    .setAction("UNDO", component1 -> {
                        LogUtil.loge("onClick triggered");
                    });
            snackbar.setActionTextColor(ResourceTable.Color_ltgray);
            snackbar.setIconLeft(ResourceTable.Media_ic_core, 24);
            Component snackbarView = snackbar.getView();
            ShapeElement background = new ShapeElement();
            background.setRgbColor(RgbColor.fromRgbaInt(Color.getIntColor("#555555ff")));
            snackbarView.setBackground(background);
            Text textView = (Text) snackbarView.findComponentById(ResourceTable.Id_snackbar_text);
            textView.setTextColor(Color.WHITE);
            snackbar.show();
        });
    }

    private void setButton3Click() {
        button3.setClickedListener(component -> {
            TSnackbar snackbar = TSnackbar
                    .make(mDependentLayout, "Had a snack at Snackbar", TSnackbar.LENGTH_LONG)
                    .setAction("ACTION", component1 -> {
                        LogUtil.loge("CLICKED Action");
                    });
            snackbar.setActionTextColor(ResourceTable.Color_white);
            Component snackbarView = snackbar.getView();
            ShapeElement background = new ShapeElement();
            background.setRgbColor(RgbColor.fromRgbaInt(Color.getIntColor("#0000CCff")));
            snackbarView.setBackground(background);
            Text textView = (Text) snackbarView.findComponentById(ResourceTable.Id_snackbar_text);
            textView.setTextColor(Color.WHITE);
            snackbar.show();
        });
    }

    private void setButton4Click() {
        button4.setClickedListener(component -> {
            TSnackbar snackbar = TSnackbar.make(mDependentLayout, "Had a snack at Snackbar  " +
                    "Had a snack at Snackbar  Had a snack at Snackbar Had a snack at Snackbar " +
                    "Had a snack at Snackbar Had a snack at Snackbar", TSnackbar.LENGTH_LONG);
            snackbar.setActionTextColor(ResourceTable.Color_white);
            Component snackbarView = snackbar.getView();
            ShapeElement background = new ShapeElement();
            background.setRgbColor(RgbColor.fromRgbaInt(Color.getIntColor("#CC00CCff")));
            snackbarView.setBackground(background);
            Text textView = (Text) snackbarView.findComponentById(ResourceTable.Id_snackbar_text);
            textView.setTextColor(Color.YELLOW);
            snackbar.show();
        });
    }

    private void setButton5Click() {
        button5.setClickedListener(component -> {
            TSnackbar snackbar = TSnackbar
                    .make(mDependentLayout, "Snacking with VectorDrawable", TSnackbar.LENGTH_LONG);
            snackbar.setActionTextColor(ResourceTable.Color_white);
            snackbar.setIconLeft(ResourceTable.Media_ic_core, 24);
            Component snackbarView = snackbar.getView();
            ShapeElement background = new ShapeElement();
            background.setRgbColor(RgbColor.fromRgbaInt(Color.getIntColor("#CC00CCff")));
            snackbarView.setBackground(background);
            Text textView = (Text) snackbarView.findComponentById(ResourceTable.Id_snackbar_text);
            textView.setTextColor(Color.YELLOW);
            snackbar.show();
        });
    }

    private void setButton6Click() {
        button6.setClickedListener(component -> {
            TSnackbar snackbar = TSnackbar
                    .make(mDependentLayout, "Snacking Left & Right", TSnackbar.LENGTH_LONG);
            snackbar.setActionTextColor(ResourceTable.Color_white);
            // Size in dp - 24 is great!
            snackbar.setIconLeft(ResourceTable.Media_ic_core, 24);
            // Resize to bigger dp
            snackbar.setIconRight(ResourceTable.Media_icon, 48);
            snackbar.setIconPadding(8);
            snackbar.setMaxWidth(3000);
            Component snackbarView = snackbar.getView();
            ShapeElement background = new ShapeElement();
            background.setRgbColor(RgbColor.fromRgbaInt(Color.getIntColor("#CC00CCff")));
            snackbarView.setBackground(background);
            Text textView = (Text) snackbarView.findComponentById(ResourceTable.Id_snackbar_text);
            textView.setTextColor(Color.YELLOW);
            snackbar.show();
        });
    }

    private void setButton7Click() {
        button7.setClickedListener(component -> {
            Intent secondIntent = new Intent();
            // 指定待启动FA的bundleName和abilityName
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName("com.ohosadvance.tsnackbar")
                    .withAbilityName("com.ohosadvance.tsnackbar.ToolbarAbilityExample")
                    .build();
            secondIntent.setOperation(operation);
            startAbility(secondIntent);
        });
    }
}
